package com.chengxiaoxiao.main;
/**
* @author ChengXiaoXiao
*
* 2018年10月28日下午2:05:12
*/

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
	public static void main(String[] args) {
		// get factory instance
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

		try {
			DocumentBuilder builder = dbf.newDocumentBuilder();
			Document document = builder.parse(new File("src/student.xml"));

			NodeList listStudent = document.getElementsByTagName("student");
			for (int i = 0; i < listStudent.getLength(); i++) {
				Element element = (Element) listStudent.item(i);
				// get Attribute
				String stuNumber = element.getAttribute("stuNumber");

				// get children Node
				NodeList childNodes = element.getChildNodes();
				for (int j = 0; j < childNodes.getLength(); j++) {
					Node node = childNodes.item(j);
					if (node instanceof Element) {
						String nodeName = node.getNodeName();
						String textContent = node.getTextContent();
						System.out.print("stuNumber:" + stuNumber + "," + nodeName + ":" + textContent + "\n");
					}

				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
